﻿using System;
using System.Collections.Generic;

using NUnit.Framework;

using VidoopSecure;
using VidoopSecureExtensionMethods;

namespace TestVidoopSecure
{
    [TestFixture]
    public class TestImageShieldService : BaseTest
    {
        [TestFixtureSetUp]
        public override void SetUp()
        {
            base.SetUp();
        }

        [TestFixtureTearDown]
        public override void TearDown()
        {
            base.TearDown();
        }

        [Test]
        public void Test_GetService()
        {
            Service imageshieldService = Manager.GetService(
                "imageshield", CurrentSite);
            Assert.IsNotNull(imageshieldService);
        }

        [Test]
        public void Test_NewImageShield()
        {
            ImageShieldService service = (ImageShieldService)Manager.GetService(
                "imageshield", CurrentSite);
            Assert.IsNotNull(service);

            string[] categories = { "cars0001", "people01", "lksnkys1", "wldanml1",
            "castles1", "houses01", "trains01", "flowers1", "dogs0001", "food0001",
            "skyscrp1", "stndgls1", "muscins1", "tysngms1", "cats0001", "bevrags1",
            "insects1", "comptrs1", "outspac1", "birds001", "boats001", "airplns1",
            "undrwtr1", "money001", "telphns1", "clocks01" };

            string bundle = string.Join(":", categories.Slice(0, 12));
            
            int length = 3;
            bool orderMatters = true;
            int width = 4;
            int height = 3;
            string imageCodeColor = "Red";
            string userId = "abc123";
            
            ImageShield shield = service.NewImageShield(length, orderMatters,
                width, height, imageCodeColor, userId, bundle);

            Assert.IsNotNull(shield.Id);
            Assert.AreEqual(length, shield.Length);
            Assert.AreEqual(orderMatters, shield.OrderMatters);
            Assert.AreEqual(width, shield.Width);
            Assert.AreEqual(height, shield.Height);
            Assert.AreEqual(imageCodeColor, shield.ImageCodeColor);
            Assert.AreEqual(Settings.ApiBase + "/imageshields/" + shield.Id + "/image", shield.Image);
            Assert.IsFalse(shield.Attempted);
            Assert.IsFalse(shield.Authenticated);

            // attempt code verification
            bool verifyresult = service.VerifyCode(shield.Id, "a");
            Assert.IsFalse(verifyresult);

            // see that verify attempt has been recorded
            ImageShield shield2 = service.GetImageShield(shield.Id);
            Assert.AreEqual(shield.Id, shield2.Id);
            Assert.IsTrue(shield2.Attempted);
            Assert.IsFalse(shield2.Authenticated);
        }
    }
}